
function ApiTaskQueue (loadingObject){
  this.loading = '';
  this.push_task = [];
  this.api_cancel_token = {};
  this.loadingObject = loadingObject;
}
// 创建loading
ApiTaskQueue.prototype.startLoading = function () {
    this.loading = this.loadingObject.service({
        lock: true,
        text: '加载中……',
        customClass:'task-full-loading',
        background: 'rgba(0, 0, 0, 0.1)'
    })
};
ApiTaskQueue.prototype.endLoading = function (){
    this.loading ? this.loading.close() :'';
};
// 保存请求的任务id
ApiTaskQueue.prototype.addCancelTokenTask = function(url,cancel){
  this.api_cancel_token[url.name] = {
    ...url,
    cancel
  }
},
// 删除请求的任务id
ApiTaskQueue.prototype.removeCancelTokenTask = function(url){
  delete this.api_cancel_token[url.name];
},
// 添加更新loading队列
ApiTaskQueue.prototype.showFullScreenLoading = function (task) {
    if (this.push_task.length === 0) {
        this.startLoading();
    }
    this.push_task.push(task);
    this.updatatasks();
};
// 删除任务队列
ApiTaskQueue.prototype.tryHideFullScreenLoading = function(task = {name:''}) {
  if (typeof task === 'string'){
    this.endLoading();
    this.push_task.length = 0;
    return false;
  }
  if (this.push_task.length > 0){
    for (let i = 0; i < this.push_task.length; i++) {
      const item = this.push_task[i];
      if (item.name === task.name) {
        this.push_task.splice(i, 1);
        break;
      }
    }
  }
  if (this.push_task.length === 0) {
    this.endLoading();
  }
  this.updatatasks();
};
// 更新队列
ApiTaskQueue.prototype.updatatasks = function(){
  let ele = '';
  if (process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'production'){
    ele = document.getElementsByClassName('task-full-loading');
  }
  if (ele) {
    let a = ele[0],
        str = ``;
    for (let i = 0; i < this.push_task.length; i++) {
        const item = this.push_task[i];
        str += `<p class="tipsred" style="margin-right:20px;">${i + 1}、${item.value}</>`;
    }
    let div = a.getElementsByTagName('div')[0];
    // 判断是存在子元素
    let c_div = div.getElementsByTagName('div');
    let newele;
    if (c_div && c_div.length > 0){
      newele = c_div[0];
      newele.innerHTML = str;
    } else {
      newele = document.createElement('div');
      newele.innerHTML = str;
      div.appendChild(newele);
    }
  }
};
export {
    ApiTaskQueue
}
